package com.phale.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.log.Log;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.phale.domain.Item;
import com.phale.domain.ItemDao;
import com.phale.domain.utils.ItemFactory;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

	private BlobstoreService blobstoreService = BlobstoreServiceFactory
			.getBlobstoreService();
	private ImagesService imagesService = ImagesServiceFactory
			.getImagesService();

	@RequestMapping(method = RequestMethod.GET, value = "/index")
	public String index(ModelMap model) {

		String uploadUrl = blobstoreService.createUploadUrl("/admin/save");
		model.addAttribute("uploadUrl", uploadUrl);
		return getView("index");
	}

	@RequestMapping(method = RequestMethod.GET, value = "/view")
	public String view(ModelMap model) {
		List<Item> items = ItemDao.findAllItems();
		model.addAttribute("items", items);

		return getView("result");
	}

	@RequestMapping(method = RequestMethod.POST, value = "/save")
	public String save(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request);
		List<BlobKey> blobKey = blobs.get("file");

		if (blobKey == null) {
			model.addAttribute("message", "Noname");
		} else {
			model.addAttribute("message", "uploaded "
					+ blobKey.get(0).getKeyString() + " file(s)");
		}

		ItemFactory.get().buildItemFromRequest(request);

		List<Item> items = ItemDao.findAllItems();
		model.addAttribute("items", items);

		return getView("result");
	}

	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "/serve")
	public String serve(HttpServletRequest request, HttpServletResponse response) {
		BlobKey blobKey = new BlobKey(request.getParameter("blob-key"));
		try {
			blobstoreService.serve(blobKey, response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "/image")
	public String image(HttpServletRequest request, HttpServletResponse response) {
		try {
			String itemId = request.getParameter("itemId");
			Item item = ItemDao.findItem(itemId);
			response.setContentType("image/jpeg");
			byte[] bs = item.getImageRz().getBytes();

			ServletOutputStream out = response.getOutputStream();

			out.write(bs, 0, bs.length);
			out.flush();
			out.close();
		} catch (IOException e) {
			Log.debug(e);
		}
		return null;
	}
}
